#include <cctype>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <string>

using namespace std;

bool my_alpha(string &word){
    for(auto it=word.begin();it!=word.end();it++){
        if(!isalpha(*it))
            return false;
    }
    return true;
}

class TextContent{
public:
    TextContent(){
        text.reserve(50);
    }

    ~TextContent(){

    }

    void readFile(const string &filename){
        ifstream ifs(filename);
        string line;
        string word;
        while(getline(ifs,line)){
            istringstream iss(line);
            text.emplace_back(line);
            while(iss>>word){
                if(my_alpha(word)){
                lineContent[word].insert(text.size()-1);
                wordCount[word]++;
            }
            } 
        }
    }

    void getWord(const string &word){
            cout<<" -- "<<word<<" occur "<<wordCount[word]<<" times -- "<<endl;
            for(auto it=lineContent[word].begin();it!=lineContent[word].end();it++){
                cout<<" -- line:"<<(*it)+1<<" \t|| "<< text[(*it)] <<endl;
            }

    }

private:
    vector<string> text;
    map<string,set<int>> lineContent;
    map<string,int> wordCount;
};

void test(){
    string readfile="./china.txt";
    TextContent tc;
    tc.readFile(readfile);

    string findWord="the";
    tc.getWord(findWord);

}

int main()
{
 test();
 return 0;
}

